package cn.walls1717.wallsmall.product.service.impl;

import cn.walls1717.wallsmall.common.utils.PageUtils;
import cn.walls1717.wallsmall.common.utils.Query;
import cn.walls1717.wallsmall.product.dao.SpuImagesDao;
import cn.walls1717.wallsmall.product.entity.SpuImagesEntity;
import cn.walls1717.wallsmall.product.service.SpuImagesService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("spuImagesService")
public class SpuImagesServiceImpl extends ServiceImpl<SpuImagesDao, SpuImagesEntity> implements SpuImagesService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuImagesEntity> page =
            this.page(new Query<SpuImagesEntity>().getPage(params), new QueryWrapper<SpuImagesEntity>());

        return new PageUtils(page);
    }

    /**
     * 批量保存spu图片
     *
     * @param spuId spuId
     * @param images 图片集合
     */
    @Override
    public void saveBatchSpuImages(Long spuId, List<String> images) {
        if (images == null || images.size() == 0) {
            return;
        }
        List<SpuImagesEntity> spuImages = images.stream().map(img -> {
            SpuImagesEntity spuImage = new SpuImagesEntity();
            spuImage.setSpuId(spuId);
            spuImage.setImgUrl(img);
            return spuImage;
        }).collect(Collectors.toList());
        saveBatch(spuImages);
    }
}